package com.ngu.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

/**
 * Created by Hqq on 2019/10/2. 拦截器
 */
@Configuration
public class WebSecurityConfig extends WebMvcConfigurerAdapter {
    @Value("${ueditor.upload-path}")
    private String path;

    public static final String SESSION_KEY = "user";

    @Bean
    public LoginInterceptor  getSecurityInterceptor() {
        return new LoginInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration addInterceptor = registry.addInterceptor(getSecurityInterceptor());
        // 排除配置--对下面的不进行拦截
        addInterceptor.excludePathPatterns("/sys/login");
        addInterceptor.excludePathPatterns("/sys/logout");
        addInterceptor.excludePathPatterns("/start/**");
        // 拦截配置
        addInterceptor.addPathPatterns("/**");
    }

    /**
     * 配置静态文件
     * 如果我们将/xxxx/** 修改为 /** 与默认的相同时，则会覆盖系统的配置，可以多次使用 addResourceLocations 添加目录，
     * 优先级先添加的高于后添加的。
     *
     * 如果是/xxxx/** 引用静态资源 加不加/xxxx/ 均可，因为系统默认配置（/**）也会作用
     * 如果是/** 会覆盖默认配置，应用addResourceLocations添加所有会用到的静态资源地址，系统默认不会再起作用
     */

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/fileURL/**").addResourceLocations("file:"+path.replaceAll("\\\\",""));
        super.addResourceHandlers(registry);
        //静态文件位置
        ResourcesFileUtils.path = path.replaceAll("\\\\","");
    }
}
